package com.wangyang.stock.dao.hibernate;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.wangyang.stock.dao.IDao;
import com.wangyang.stock.vo.MerInvVo;

public class DaoImp implements IDao {

	private Session session;
	private Transaction transaction;
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * 添加对象
	 * 
	 * @param obj
	 * @return
	 */
	public boolean create(Object obj) {
		boolean is = false;
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		try {
			session.save(obj);
			is = true;
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return is;
	}

	/**
	 * 删除对象
	 * 
	 * @param obj
	 * @return
	 */
	public boolean delete(Object obj) {
		boolean is = false;
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		try {
			session.delete(obj);
			is = true;
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return is;
	}

	/**
	 * 更新对象
	 * 
	 * @param obj
	 * @return
	 */
	public boolean update(Object obj) {
		boolean is = false;
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		try {
			session.update(obj);
			is = true;
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return is;
	}

	/**
	 * 查找对象
	 * 
	 * @param obj
	 * @return
	 */
	public List select(String hql) {

		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		List list = null;
		try {
			Query query = session.createQuery(hql);
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return list;
	}

	public List selectBySQL(String sql) {
		// TODO Auto-generated method stub
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		List list = null;
		try {
			list = new ArrayList();
			PreparedStatement pstate = session.connection().prepareStatement(sql);
			pstate.execute();
			ResultSet rs = pstate.getResultSet();
			while (rs.next()) {
				MerInvVo merInvVo = new MerInvVo();
				merInvVo.setMerchandiseName(rs.getString("name"));
				merInvVo.setMerchandiseAmount(rs.getInt("amount"));
				list.add(merInvVo);
			}
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			session.flush();
			transaction.commit();
			session.close();
		}
		return list;
	}
}
